﻿#include "FITKAbaqusAdaptorMaterialDamageInitiation.h"
#include "InpKeyLineSpliter.h"

#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterial.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialJohnsonCookDamage.h"

namespace IO
{
    QString FITKAbaqusAdaptorMaterialDamageInitiation::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorMaterialDamageInitiation";
    }

    bool FITKAbaqusAdaptorMaterialDamageInitiation::adaptR()
    {
        
        _materialData = dynamic_cast<Interface::FITKAbaMaterial*>(_dataObj);
        if (!_materialData || !_reader) return false;

        //回退一行 处理关键字信息 校验
        _reader->backLine();
        bool readOK = false;
        QString line = _reader->readLine();
        InpKeyLineSpliter s(line, _reader);
        if (!line.toLower().startsWith("*damage initiation")) return false;
        //类型
        Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType materialType = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType::None;
        QString criterion = s["criterion"].toUpper();
        if (criterion == "JOHNSON COOK")
        {
            materialType = Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType::JohnsonCookDamage;
            //创建JohnsonCookDamage行为
            bool create = _materialData->addMaterialBehavior(materialType);
            if (!create)return false;
            readOK = this->readTypeJohnsonCookDamage();
        }
        if (!readOK)
            _materialData->removeMaterialBehavior(materialType);
        return readOK;
    }

    bool FITKAbaqusAdaptorMaterialDamageInitiation::adaptW()
    {
        _materialData = dynamic_cast<Interface::FITKAbaMaterial*>(_dataObj);
        if (!_materialData || !_writer) return false;
        //获取
        bool writeOK = true;
        writeOK &= this->writeTypeJohnsonCookDamage();
        return writeOK;
    }

    bool FITKAbaqusAdaptorMaterialDamageInitiation::readTypeJohnsonCookDamage()
    {
        if (!_materialData || !_reader)return false;
        //获取数据
        Interface::FITKAbaMaterialJohnsonCookDamage* johnsonCookDamage = dynamic_cast<Interface::FITKAbaMaterialJohnsonCookDamage*>
            (_materialData->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType::JohnsonCookDamage));
        if (!johnsonCookDamage)return false;
        //处理数据
        QString line = _reader->readLine();
        QStringList str = line.split(',');
        if (str.size() < 8)return false;
        //设置数据
        bool ok = false;
        //D1
        johnsonCookDamage->setD1(str.at(0).trimmed().toDouble(&ok));
        if (!ok)return false;
        //D2
        johnsonCookDamage->setD2(str.at(1).trimmed().toDouble(&ok));
        if (!ok)return false;
        //D3
        johnsonCookDamage->setD3(str.at(2).trimmed().toDouble(&ok));
        if (!ok)return false;
        //D4
        johnsonCookDamage->setD4(str.at(3).trimmed().toDouble(&ok));
        if (!ok)return false;
        //D5
        johnsonCookDamage->setD5(str.at(4).trimmed().toDouble(&ok));
        if (!ok)return false;
        //熔点
        johnsonCookDamage->setMeltingTemperature(str.at(5).trimmed().toDouble(&ok));
        if (!ok)return false;
        //转换温度
        johnsonCookDamage->setTransitionTemperature(str.at(6).trimmed().toDouble(&ok));
        if (!ok)return false;
        //参考应变率
        johnsonCookDamage->setReferenceStrainRate(str.at(7).trimmed().toDouble(&ok));
        if (!ok)return false;
        line = _reader->readLine();
        return ok;
    }

    bool FITKAbaqusAdaptorMaterialDamageInitiation::writeTypeJohnsonCookDamage()
    {
        if (!_materialData || !_writer)return false;
        //获取数据
        Interface::FITKAbaMaterialJohnsonCookDamage* johnsonCookDamage = dynamic_cast<Interface::FITKAbaMaterialJohnsonCookDamage*>
            (_materialData->getMaterialBehavior(Interface::FITKAbaMaterialBehaviorEnum::FITKAbaMaterialBehaviorType::JohnsonCookDamage));
        if (!johnsonCookDamage)return true;
        QTextStream* stream = _writer->getStream();
        QString lineOne = "*Damage Initiation, criterion=JOHNSON COOK";
        *stream << lineOne << endl;
        //获取D1
        double d1 = johnsonCookDamage->getD1();
        //获取D2
        double d2 = johnsonCookDamage->getD2();
        //获取D3
        double d3 = johnsonCookDamage->getD3();
        //获取D4
        double d4 = johnsonCookDamage->getD4();
        //获取D5
        double d5 = johnsonCookDamage->getD5();
        //获取熔点
        double p = johnsonCookDamage->getMeltingTemperature();
        //获取转换温度
        double t = johnsonCookDamage->getTransitionTemperature();
        //获取参考应变率
        double r = johnsonCookDamage->getReferenceStrainRate();
        //写出数据
        lineOne = QString("%1, %2, %3, %4, %5, %6, %7, %8").arg(_writer->double2String(d1, 7, 0)).arg(_writer->double2String(d2, 7, 0))
            .arg(_writer->double2String(d3, 7, 0)).arg(_writer->double2String(d4, 7, 0)).arg(_writer->double2String(d5, 7, 0))
            .arg(_writer->double2String(p, 7, 0)).arg(_writer->double2String(t, 7, 0)).arg(_writer->double2String(r, 7, 0));
        *stream << lineOne << endl;
        return true;
    }
    
}
